<<<<<<< HEAD
/*/////////////////////////////////////////////////////////////////////////////
//           Version 24.07.2012
//
//  purposes:  Match temperature from a logfile to calibration polynomials of textfile
//               (more generally: match data point from logfile to spectra)
// 
//            The problem is, the logfiles tend to have much higher resolution. We need to downsample
//
//     Current script is not perfect. It just matches to within 1 minute.
//      Assumption: temperature resolution is not needed better than 1 minute.
// 
//     WHAT YOU NEED:
//     PolyInputFile     : text log file of polynomials    -- Columns: FileNumber, Time, Poly[0], Poly[1], Poly[2]
//     TempInputFile     : text log file with temperatures -- Columns: Date, Time, Temp
//    
///////////////////////////////////////////////////////////////////////////////
// by Eric Gentry
// questions: Tilman.Hueneke@iup.uni-heidelberg.de
//            egentry@mit.edu
/////////////////////////////////////////////////////////////////////////////*/

import System
import DoasCore
import DoasCore.Spectra
import DoasCore.Math
import DoasCore.IO
import DoasCore.Script
import DoasCore.HMI

// ========== UPDATE EACH RUN ==========

var PolyInputFile =  "C:\\PolyData.dat"
var TempInputFile =  "C:\\TempData.dat"

var OutputFile    =  "C:\\PolyTempPair.dat"

var bSingleRun            = false     //Only do one 1 spectrum?

 //*****************************************************************************************//

function main()
{
    Console.WriteLine("Started main")
    
    var PolyObject   = new Object()
    var TempObject   = new Object()
    var OutputObject = new Object()
    
    Console.WriteLine("Starting PolyParse")
    
    PolyObject   = PolyParse(PolyInputFile)

    Console.WriteLine("Starting TempParse")
    TempObject   = TempParse(TempInputFile)
    
    Console.WriteLine("Starting PairMatching")
    OutputObject = PairMatching(PolyObject, TempObject)
                                
    Console.WriteLine("Starting ExportOutput")
    ExportOutput(OutputObject, OutputFile)
   
    Console.WriteLine("Script: 'Poly_Temp_Pairing.js' complete!")
   
    return
}
            
 //*****************************************************************************************//  
    
    
function PolyParse(InputFile)
//  Uses an ActiveX object to give JavaScript the ability to read a text file
//  First line is discarded (header information)
//  Further lines are space-delimited.
//  Returns an array of Objects
    {
     var ReadFileSystem = new ActiveXObject("Scripting.FileSystemObject")
     var infile         = ReadFileSystem.GetFile(InputFile)
     var infileStream   = infile.OpenAsTextStream(1, 0)

     var TemporaryArray   = new Array()
     var OutputObject     = new Object()
   
     for(var count = 0; !infileStream.AtEndOfStream; count++)
     {
        if(count == 0)
          {infileStream.ReadLine(); continue}
        else
          TemporaryArray[count-1] = infileStream.ReadLine().split(" ")
     } 
     
     infileStream.Close()
    
     var overlapCounter = 0
     
     for(var index in TemporaryArray)
        {
          if(TemporaryArray[index] == "")     //skip empty lines in textfile
            continue;
            
          var TempRunInfoObject = {"FileNumber" : parseInt(TemporaryArray[index][0]),
                                   "Time"       : TemporaryArray[index][1].substring(4,9),
                                   "Poly0"      : parseFloat(TemporaryArray[index][2]),
                                   "Poly1"      : parseFloat(TemporaryArray[index][3]),
                                   "Poly2"      : parseFloat(TemporaryArray[index][4])}
          
          if(OutputObject[TempRunInfoObject.Time] != undefined)
            {overlapCounter++
             continue}
                                   
          OutputObject[TempRunInfoObject.Time] = TempRunInfoObject                               
        }
        Console.WriteLine("Polynomial Overlap Count: " + overlapCounter)
        
        //Getting an "Object Expected" Error? Your input file doesn't have enough information!
     
     return OutputObject
}  
     
//*****************************************************************************************// 
     
    
function TempParse(InputFile)
//  Uses an ActiveX object to give JavaScript the ability to read a text file
//  First line is discarded (header information)
//  Further lines are space-delimited.
//  Returns an array of Objects
    {
     var ReadFileSystem = new ActiveXObject("Scripting.FileSystemObject")
     var infile         = ReadFileSystem.GetFile(InputFile)
     var infileStream   = infile.OpenAsTextStream(1, 0)

     var TemporaryArray   = new Array()
     var OutputObject     = new Object()
   
     for(var count = 0; !infileStream.AtEndOfStream; count++)
     {
        if(count == 0)
          {infileStream.ReadLine(); continue}
        else
          TemporaryArray[count-1] = infileStream.ReadLine().split(" ")
     } 
     
     infileStream.Close()
  
     
     for(var index in TemporaryArray)
        {
          if(TemporaryArray[index] == "")     //skip empty lines in textfile
            continue;
            
          var TempRunInfoObject = {"Time"       : TemporaryArray[index][1].substring(0,5),
                                   "Temp"       : parseFloat(TemporaryArray[index][2])}
          
          if(OutputObject[TempRunInfoObject.Time] != undefined)
            {continue}
                                   
          OutputObject[TempRunInfoObject.Time] = TempRunInfoObject                               
        }

        
        //Getting an "Object Expected" Error? Your input file doesn't have enough information!
     
     return OutputObject
} 
    
//*****************************************************************************************// 

function PairMatching(PolyObject, TempObject)
{
    var OutputObject = new Object()
    
    for(var time in PolyObject)
    {
        
        var CombinedInfo = {"Time"       : time,
                            "Temp"       : TempObject[time].Temp,
                            "Poly0"      : parseFloat(PolyObject[time].Poly0),
                            "Poly1"      : parseFloat(PolyObject[time].Poly1),
                            "Poly2"      : parseFloat(PolyObject[time].Poly2)}
                                       
        
        OutputObject[time] = CombinedInfo 
        
    
    }

    return OutputObject
}

//*****************************************************************************************//
   function ExportOutput(OutputObject, OutputFile)
   {
       var dispFileSystem   = new ActiveXObject("Scripting.FileSystemObject")
       var outfile  = dispFileSystem.OpenTextFile(OutputFile,2, true)
       outfile.WriteLine("Time\t" + "Temp\t" + "Poly0\t" + "Poly1\t" + "Poly2")
       
       for(var time in OutputObject)
       {
          outfile.WriteLine(OutputObject[time].Time + "\t" + OutputObject[time].Temp + "\t" + OutputObject[time].Poly0 + "\t" + OutputObject[time].Poly1 + "\t" + OutputObject[time].Poly2)
       }
 
 
    outfile.Close()
   }
   
//*********************************************************************************** 
	    
=======
/*/////////////////////////////////////////////////////////////////////////////
//           Version 24.07.2012
//
//  purposes:  Match temperature from a logfile to calibration polynomials of textfile
//               (more generally: match data point from logfile to spectra)
// 
//            The problem is, the logfiles tend to have much higher resolution. We need to downsample
//
//     Current script is not perfect. It just matches to within 1 minute.
//      Assumption: temperature resolution is not needed better than 1 minute.
// 
//     WHAT YOU NEED:
//     PolyInputFile     : text log file of polynomials    -- Columns: FileNumber, Time, Poly[0], Poly[1], Poly[2]
//     TempInputFile     : text log file with temperatures -- Columns: Date, Time, Temp
//    
///////////////////////////////////////////////////////////////////////////////
// by Eric Gentry
// questions: Tilman.Hueneke@iup.uni-heidelberg.de
//            egentry@mit.edu
/////////////////////////////////////////////////////////////////////////////*/

import System
import DoasCore
import DoasCore.Spectra
import DoasCore.Math
import DoasCore.IO
import DoasCore.Script
import DoasCore.HMI

// ========== UPDATE EACH RUN ==========

var PolyInputFile =  "C:\\PolyData.dat"
var TempInputFile =  "C:\\TempData.dat"

var OutputFile    =  "C:\\PolyTempPair.dat"

var bSingleRun            = false     //Only do one 1 spectrum?

 //*****************************************************************************************//

function main()
{
    Console.WriteLine("Started main")
    
    var PolyObject   = new Object()
    var TempObject   = new Object()
    var OutputObject = new Object()
    
    Console.WriteLine("Starting PolyParse")
    
    PolyObject   = PolyParse(PolyInputFile)

    Console.WriteLine("Starting TempParse")
    TempObject   = TempParse(TempInputFile)
    
    Console.WriteLine("Starting PairMatching")
    OutputObject = PairMatching(PolyObject, TempObject)
                                
    Console.WriteLine("Starting ExportOutput")
    ExportOutput(OutputObject, OutputFile)
   
    Console.WriteLine("Script: 'Poly_Temp_Pairing.js' complete!")
   
    return
}
            
 //*****************************************************************************************//  
    
    
function PolyParse(InputFile)
//  Uses an ActiveX object to give JavaScript the ability to read a text file
//  First line is discarded (header information)
//  Further lines are space-delimited.
//  Returns an array of Objects
    {
     var ReadFileSystem = new ActiveXObject("Scripting.FileSystemObject")
     var infile         = ReadFileSystem.GetFile(InputFile)
     var infileStream   = infile.OpenAsTextStream(1, 0)

     var TemporaryArray   = new Array()
     var OutputObject     = new Object()
   
     for(var count = 0; !infileStream.AtEndOfStream; count++)
     {
        if(count == 0)
          {infileStream.ReadLine(); continue}
        else
          TemporaryArray[count-1] = infileStream.ReadLine().split(" ")
     } 
     
     infileStream.Close()
    
     var overlapCounter = 0
     
     for(var index in TemporaryArray)
        {
          if(TemporaryArray[index] == "")     //skip empty lines in textfile
            continue;
            
          var TempRunInfoObject = {"FileNumber" : parseInt(TemporaryArray[index][0]),
                                   "Time"       : TemporaryArray[index][1].substring(4,9),
                                   "Poly0"      : parseFloat(TemporaryArray[index][2]),
                                   "Poly1"      : parseFloat(TemporaryArray[index][3]),
                                   "Poly2"      : parseFloat(TemporaryArray[index][4])}
          
          if(OutputObject[TempRunInfoObject.Time] != undefined)
            {overlapCounter++
             continue}
                                   
          OutputObject[TempRunInfoObject.Time] = TempRunInfoObject                               
        }
        Console.WriteLine("Polynomial Overlap Count: " + overlapCounter)
        
        //Getting an "Object Expected" Error? Your input file doesn't have enough information!
     
     return OutputObject
}  
     
//*****************************************************************************************// 
     
    
function TempParse(InputFile)
//  Uses an ActiveX object to give JavaScript the ability to read a text file
//  First line is discarded (header information)
//  Further lines are space-delimited.
//  Returns an array of Objects
    {
     var ReadFileSystem = new ActiveXObject("Scripting.FileSystemObject")
     var infile         = ReadFileSystem.GetFile(InputFile)
     var infileStream   = infile.OpenAsTextStream(1, 0)

     var TemporaryArray   = new Array()
     var OutputObject     = new Object()
   
     for(var count = 0; !infileStream.AtEndOfStream; count++)
     {
        if(count == 0)
          {infileStream.ReadLine(); continue}
        else
          TemporaryArray[count-1] = infileStream.ReadLine().split(" ")
     } 
     
     infileStream.Close()
  
     
     for(var index in TemporaryArray)
        {
          if(TemporaryArray[index] == "")     //skip empty lines in textfile
            continue;
            
          var TempRunInfoObject = {"Time"       : TemporaryArray[index][1].substring(0,5),
                                   "Temp"       : parseFloat(TemporaryArray[index][2])}
          
          if(OutputObject[TempRunInfoObject.Time] != undefined)
            {continue}
                                   
          OutputObject[TempRunInfoObject.Time] = TempRunInfoObject                               
        }

        
        //Getting an "Object Expected" Error? Your input file doesn't have enough information!
     
     return OutputObject
} 
    
//*****************************************************************************************// 

function PairMatching(PolyObject, TempObject)
{
    var OutputObject = new Object()
    
    for(var time in PolyObject)
    {
        
        var CombinedInfo = {"Time"       : time,
                            "Temp"       : TempObject[time].Temp,
                            "Poly0"      : parseFloat(PolyObject[time].Poly0),
                            "Poly1"      : parseFloat(PolyObject[time].Poly1),
                            "Poly2"      : parseFloat(PolyObject[time].Poly2)}
                                       
        
        OutputObject[time] = CombinedInfo 
        
    
    }

    return OutputObject
}

//*****************************************************************************************//
   function ExportOutput(OutputObject, OutputFile)
   {
       var dispFileSystem   = new ActiveXObject("Scripting.FileSystemObject")
       var outfile  = dispFileSystem.OpenTextFile(OutputFile,2, true)
       outfile.WriteLine("Time\t" + "Temp\t" + "Poly0\t" + "Poly1\t" + "Poly2")
       
       for(var time in OutputObject)
       {
          outfile.WriteLine(OutputObject[time].Time + "\t" + OutputObject[time].Temp + "\t" + OutputObject[time].Poly0 + "\t" + OutputObject[time].Poly1 + "\t" + OutputObject[time].Poly2)
       }
 
 
    outfile.Close()
   }
   
//*********************************************************************************** 
	    
>>>>>>> Updated "ExtractSpectraParameters.js"
main();